home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  4.0 KB  |  194 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        main.c
  3.  
  4.     Contains:    Main event loop and basic keyboard/mouse processing
  5.  
  6.     Written by:    Richard Clark
  7.  
  8.     Copyright:    © 1993-1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                  2/16/94    RC        Modified suspend/resume code to create a "dummy" activate/deactivate
  13.                                      event record.
  14.                   2/9/94    RC        Added test to omit the "qd" definition if compiling under Metrowerks C
  15.                  1/26/94    RC        Added rudimentary Suspend/resume support
  16.                 11/28/93    RC        First release
  17.  
  18.     To Do:
  19. */
  20.  
  21.  
  22. #ifndef __MEMORY__
  23.     #include <Memory.h>
  24. #endif
  25.  
  26. #ifndef __APPLEEVENTS__
  27.     #include <AppleEvents.h>
  28. #endif
  29.  
  30. #ifndef __DIALOGS__
  31.     #include <Dialogs.h>
  32. #endif
  33.  
  34. #include <Menus.h>
  35. #include <Devices.h>
  36. #include <Events.h> 
  37.  
  38. #ifndef __WINDOWS__
  39.     #include <Windows.h>
  40. #endif
  41.  
  42. #define        __Main__
  43. #include "ModApp.h"
  44. #include "Prototypes.h"
  45.  
  46. #if !defined(__MWERKS__) // MetroWerks declares "qd" in their runtime
  47.     QDGlobals    qd;
  48. #endif
  49.  
  50. #define kIdleTime 15L        /* Take this long per null event -- 15L = 1/4 second */
  51. static long lastTime = 0;
  52.  
  53. static void DoKey (EventRecord *theEvent);
  54. static void DoMouseDown (EventRecord *theEvent);
  55. static void MainLoop(void);
  56. void main(void);
  57.  
  58. void DoKey (EventRecord *theEvent)
  59. {
  60.     char        keyPressed = (theEvent->message & charCodeMask);
  61.  
  62.     if (theEvent->modifiers & cmdKey) {
  63.             AdjustMenus();
  64.             Dispatch(MenuKey(keyPressed));        /*Command key down*/
  65.     } else {
  66.         // Pass all other keystrokes to the frontmost window
  67.         DrawingWindowPeek    aWindow = (DrawingWindowPeek)FrontWindow();
  68.         
  69.         if (aWindow /* != NULL */) {
  70.             SetPort((WindowPtr)aWindow);
  71.             if (aWindow->toolRoutines.toolKeyProc)
  72.                 CallToolWindowKeyProc(aWindow->toolRoutines.toolKeyProc, (WindowPtr)aWindow, theEvent);
  73.         }
  74.     }
  75. } /* DoKey */
  76.  
  77.  
  78.  
  79.  
  80. void DoMouseDown (EventRecord *theEvent)
  81. {
  82.     Point            globalPt = theEvent->where;
  83.     int16            windowPart;
  84.     WindowPtr        wp;
  85.  
  86.     windowPart = FindWindow(globalPt, &wp);
  87.     switch (windowPart) {
  88.         case inMenuBar: 
  89.             AdjustMenus();
  90.             Dispatch(MenuSelect(globalPt));
  91.         break;
  92.  
  93.         case inSysWindow: 
  94.             SystemClick(theEvent, wp);
  95.         break;
  96.  
  97.  
  98.         case inGoAway: 
  99.             if (TrackGoAway(wp, theEvent->where))
  100.                 CloseAWindow(wp);
  101.         break;
  102.  
  103.         case inDrag:
  104.             DoDragWindow(wp, theEvent);
  105.         break;
  106.  
  107.         case inGrow: 
  108.             DoGrowWindow(wp, theEvent);
  109.         break;
  110.         
  111.         case inContent: 
  112.             DoContentClick(wp, theEvent);
  113.         break;
  114.  
  115.         case inZoomIn:
  116.         case inZoomOut: 
  117.             DoZoomWindow(wp, theEvent, windowPart);
  118.         break;
  119.     }
  120. } /* DoMouseDown */
  121.  
  122.  
  123. void MainLoop()
  124. {
  125.     EventRecord    theEvent;
  126.     static  long    sleepTime = 1L;
  127.  
  128.     while (!gDone) {
  129.         WaitNextEvent(everyEvent, &theEvent, sleepTime, NULL);    
  130.         
  131.         switch (theEvent.what) {
  132.             case nullEvent:
  133.                 while (TickCount() <= lastTime + kIdleTime) {
  134.                     if (isUserWindow(gCurrentWindow)) {
  135.                         DoIdleWindow(gCurrentWindow);
  136.                     }
  137.                     /* Get the next window, wrapping around to the front if necessary */
  138.                     if (gCurrentWindow != NULL) {
  139.                         gCurrentWindow = (WindowPtr)(((WindowPeek)gCurrentWindow)->nextWindow);
  140.                     } else {
  141.                         /* We've hit the end of the window list */
  142.                         gCurrentWindow = FrontWindow();
  143.                         break;
  144.                     }
  145.                 }
  146.                 lastTime = TickCount();
  147.             break;
  148.  
  149.             case mouseDown: 
  150.                 DoMouseDown(&theEvent);
  151.             break;
  152.  
  153.             case keyDown:
  154.             case autoKey: 
  155.                 DoKey(&theEvent);
  156.             break;
  157.  
  158.             case activateEvt: 
  159.                 DoActivate(&theEvent);
  160.             break;
  161.  
  162.             case updateEvt:
  163.                 DoUpdate((WindowPtr)theEvent.message);
  164.             break;
  165.             
  166.             case osEvt:
  167.                 if ((theEvent.message >> 24) & suspendResumeMessage ) {    // suspend or resume
  168.                     gCurrentWindow = FrontWindow();                     // Reset the idle pointer to the front window
  169.                     /* Modify the event record to look like an activate/deactivate event */
  170.                     theEvent.modifiers = theEvent.message; /* Copy suspend/resume flag */
  171.                     theEvent.message = (long)gCurrentWindow;    
  172.                     DoActivate(&theEvent);
  173.                 }
  174.             break;
  175.             
  176.             case kHighLevelEvent:
  177.                 (void)AEProcessAppleEvent(&theEvent);
  178.         }
  179.     }
  180. } /* MainEventLoop */
  181.  
  182.  
  183. void main()
  184. {
  185.     MaxApplZone();
  186.     MoreMasters();
  187.     MoreMasters();
  188.     MoreMasters();
  189.     MoreMasters();
  190.     
  191.     InitAll();                    /* call initialization routine    */
  192.     AdjustMenus();
  193.     MainLoop();
  194. }